﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace lzsoft.CDN
{
    public class ImageFormat
    {
        static byte[] JPG = new byte[] { 0xFF, 0xD8, 0xFF };
        static byte[] BMP = new byte[] { 0x42, 0x4D };
        static byte[] GIF = new byte[] { 0x47, 0x49, 0x46, 0x38 };
        static byte[] PNG = new byte[] { 0x89, 0x50, 0x4E, 0x47 };

        public static bool Validate(ref byte[] buffer)
        {            
            if (buffer[0] == JPG[0] && 
                buffer[1] == JPG[1] && 
                buffer[2] == JPG[2])
            {
                return true;
            }
            if (buffer[0] == PNG[0] && 
                buffer[1] == PNG[1] && 
                buffer[2] == PNG[2] && 
                buffer[3] == PNG[3])
            {
                return true;
            }
            if (buffer[0] == GIF[0] && 
                buffer[1] == GIF[1] && 
                buffer[2] == GIF[2] && 
                buffer[3] == GIF[3])
            {
                return true;
            }
            if (buffer[0] == BMP[0] && 
                buffer[1] == BMP[1])
            {
                return true;
            }
            return false;
        }
    }
}